package models

import (
	"time"

	"strconv"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)

type BuySell struct {
	Sell float64
	Buy  float64
}

func InsertPrice(Type int, Symbol string, Buy, Sell float64) {

	if Buy > 0 && Sell > 0 {
		o := orm.NewOrm()
		price := new(Price)
		price.Type = Type
		price.Symbol = Symbol
		price.Buy = Buy
		price.Sell = Sell
		price.Created = int(time.Now().Unix())

		o.Insert(price)
	} else {
		beego.Debug(Symbol+strconv.Itoa(Type)+"SaveData", "查看")
	}
}

func GetRateUSDT() float64 {

	o1 := orm.NewOrm()
	var rate Rate
	o1.QueryTable("Rate").OrderBy("-Created").One(&rate)
	return rate.Rate
}

func GetOver() float64 {
	o1 := orm.NewOrm()
	var premium Premium
	o1.QueryTable("Premium").OrderBy("-Created").One(&premium)
	return premium.Over
}

func GetDataFromMydql(symbol string) Price {

	var price Price

	o1 := orm.NewOrm()
	o1.QueryTable("Price").Filter("Symbol", symbol).Filter("Type", 3).OrderBy("-Created").One(&price)

	return price
}
